<?php

namespace app\admin\controller;

use app\common\model\Domains;
use app\common\model\Quns;
use app\common\model\Users;
use think\Request;

class Qun extends Base
{

    public function index(Request $request)
    {

        if ( $post = $request->isAjax() ){

            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 20);

            $quns = Quns::with('user')->order(['id'=>'desc'])->page($page)->limit($limit)->select();

            if ( count($quns) > 0 ){
                foreach ( $quns as $k=>$v){
                    $v['domain'] = Domains::getOneData(2);
                }
            }

            $data = array(
                'data'      => $quns,
                'count'     => Quns::count('id'),
                'code'      => 0,
                'msg'       => '',
            );
            return json($data);
        }

        return view();
    }



    // 添加、保存数据
    public function create(Request $request)
    {

        if ( $request->isPost() ){
            $post = trimAll($request->post());
            $post['alias'] = getRandStr(8);

            // 把换行，替换成||
            $post['qrcode'] = huanhangToStr($post['qrcode']);

            $post['zidingyi'] = [];
            if ( $post['muban'] == 0 ){

                if ( $post['ico'] && $post['type'] && $post['url'] ){
                    foreach ($post['ico'] as $k=>$v){

                        foreach ($post['name'] as $kk=>$vv) {

                            foreach ($post['type'] as $kkk=>$vvv){

                                foreach ($post['url'] as $kkkk=>$vvvv){

                                    if ( $k == $kk && $k == $kkk && $k == $kkkk ){
                                        if ( $v && $vvvv ){
                                            $post['zidingyi'][] = ['ico'=>$v, 'name'=>$vv, 'type'=>$vvv, 'url'=>$vvvv];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }

            $post['zidingyi'] = json_encode($post['zidingyi']);

            unset($post['touxiang']);
            unset($post['file']);
            unset($post['ico']);
            unset($post['name']);
            unset($post['type']);
            unset($post['url']);

            //写入数据库
            if ( Quns::create($post) ){
                createLog('添加微群成功：' . $post['title']);
                success('操作成功');
            }else{
                createLog('添加微群失败：' . $post['title']);
                error('操作失败');
            }

        }

        $users = Users::order(['id'=>'desc'])->select();
        $this->assign('users', $users);
        return view();
    }


    // 编辑、保存数据
    public function edit(Request $request, $id)
    {

        if ( $request->isPost() ){
            $post = trimAll($request->post());

            // 把换行，替换成||
            $post['qrcode'] = huanhangToStr($post['qrcode']);

            $post['zidingyi'] = [];
            if ( $post['muban'] == 0 ){

                if ( $post['ico'] && $post['type'] && $post['url'] ){
                    foreach ($post['ico'] as $k=>$v){

                        foreach ($post['name'] as $kk=>$vv) {

                            foreach ($post['type'] as $kkk=>$vvv){

                                foreach ($post['url'] as $kkkk=>$vvvv){

                                    if ( $k == $kk && $k == $kkk && $k == $kkkk ){
                                        if ( $v && $vvvv ){
                                            $post['zidingyi'][] = ['ico'=>$v, 'name'=>$vv, 'type'=>$vvv, 'url'=>$vvvv];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }

            $post['zidingyi'] = json_encode($post['zidingyi']);

            unset($post['touxiang']);
            unset($post['file']);
            unset($post['ico']);
            unset($post['name']);
            unset($post['type']);
            unset($post['url']);

            //写入数据库
            if ( Quns::where('id', $id)->update($post) ){
	            createLog('编辑微群成功：' . $post['title']);
                success('操作成功');
            }else{
                createLog('编辑微群失败：' . $post['title']);
                error('操作失败');
            }

        }

        //加载数据
        $data = Quns::get($id);

        // 把||，替换成换行
        $data['qrcode'] = str_replace('||', PHP_EOL, $data['qrcode']);

        $data['zidingyi'] = json_decode($data['zidingyi'], true);

        $this->assign('data', $data);

        $users = Users::order(['id'=>'desc'])->select();
        $this->assign('users', $users);
        return view();
    }


}
